/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DexterLib;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using QuartzTypeLib;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Utilities;
using System.IO;
using MyPhotoIndex.Interfaces.ImageFormatProviders;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Plugins.ImageFormatProviders
{
    public class VideoFormatProvider : ImageFormatProvider, IVideoFormatProvider
    {
        public VideoFormatProvider(string extention):
            base(extention)
        {
        }

        public override bool IsVideo
        {
            get
            {
                return true;
            }
        }

        public override bool CanSave
        {
            get { return false; }
        }

        public override Image GetThumbnail(string fileName, Size size)
        {
            IntPtr bufPtr = IntPtr.Zero;
            try
            {
                MediaDetClass md = new MediaDetClass();
                md.Filename = fileName;
                md.CurrentStream = 0;

                int bufferSize = 0;

                _AMMediaType mt = md.StreamMediaType;
                WinStructs.VIDEOINFOHEADER vih = (WinStructs.VIDEOINFOHEADER)Marshal.PtrToStructure(mt.pbFormat, typeof(WinStructs.VIDEOINFOHEADER));
                int width = vih.bmiHeader.biWidth;
                int height = vih.bmiHeader.biHeight;

                md.GetBitmapBits(0, ref bufferSize, bufPtr, width, height);
                //allocate memory
                bufPtr = Marshal.AllocHGlobal(bufferSize);
                // call to retrieve the dib
                md.GetBitmapBits(0.0, ref bufferSize, bufPtr, width, height);

                // create a bitmap object; 40 is the size of the header on a 32-bit machine
                using (Image image = new Bitmap(width, height, width * 3, PixelFormat.Format24bppRgb, new IntPtr((int)bufPtr + 40)))
                {
                    Image newImage = new Bitmap(image);
                    newImage.RotateFlip(RotateFlipType.Rotate180FlipX);

                    return newImage;
                }
            }
            catch
            {
                return null;
            }
            finally
            {
                Marshal.FreeHGlobal(bufPtr);
            }
        }

        public override bool CanRotate
        {
            get { return false; }
        }

        public override Image LoadImage(IImageData imageData, Size requestedSize)
        {
            return GetThumbnail(imageData.FileName, requestedSize);
        }

        public override void SaveImage(Image image, IImageData imageData, String fileName)
        {
            throw new NotImplementedException();
        }

        public override IImageMetaData GetImageMetaData(string fileName)
        {
           FormatProviderImageData imageData =  new FormatProviderImageData();

           try
           {
               int width = 0;
               int height = 0;
               int count = 0;

               WinStructs.VIDEOINFOHEADER vih;
               vih.bmiHeader.biSize = 0;
               while (vih.bmiHeader.biSize != 40 ||
                      count++ > 3)
               {
                   MediaDetClass md = new MediaDetClass();
                   md.Filename = fileName;

                   _AMMediaType mt = md.StreamMediaType;

                   vih = (WinStructs.VIDEOINFOHEADER)Marshal.PtrToStructure(mt.pbFormat, typeof(WinStructs.VIDEOINFOHEADER));
                   width = vih.bmiHeader.biWidth;
                   height = vih.bmiHeader.biHeight;
               }

               DateTime fileLastWriteTime = File.GetLastWriteTimeUtc(fileName);
               imageData.ImageWidth = width;
               imageData.ImageHeight = height;
               imageData.DateTaken = fileLastWriteTime;
               imageData.DateDigitized = imageData.DateTaken;

               imageData.LastModifiedDateTime = fileLastWriteTime;
           }
           catch (Exception ex)
           {
               Logger.Log(ex);
           }

           return imageData;
        }

        public override void SetImageMetaData(IImageData imageData, String fileName)
        {

        }

        #region IVideoFormatProvider Members

        public IVideoPlayer CreateVideoPlayer(string fileName, IWin32Window parent, Rectangle position)
        {
            return new VideoPlayer(fileName, parent, position);
        }

        #endregion
    }
}
